package com.example.hsh.homefragment.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.util.ArrayMap;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.hsh.homefragment.R;
import com.example.hsh.homefragment.adapter.BaseAdapter;
import com.example.hsh.homefragment.adapter.ListContentAdapter;
import com.example.hsh.homefragment.model.ListContentModel;
import com.example.hsh.homefragment.rx.presenter.RxBasePresenter;
import com.example.hsh.homefragment.utils.ScreenUtil;

import java.util.ArrayList;

import butterknife.BindView;


/**
 * Created by HSH on 2016/12/19.
 */

public class ContentListFragment extends BaseFragment {
    private static final String TAG = "ContentListFragment";
    private static final String NEED_REFRESH = "need_refresh";

    private boolean needRefresh;

    private ContentListFragmentCallBack callBack;

    @BindView(R.id.recycler_of_content)
    RecyclerView recyclerView;

    @BindView(R.id.sf_layout)
    SwipeRefreshLayout sf_layout;

    private ArrayList<ListContentModel> models = new ArrayList<>();

    public static ContentListFragment newInstance(boolean showToolbar,boolean needRefresh) {
        Bundle args = new Bundle();
        args.putBoolean(SHWO_TOOL_BAR, showToolbar);
        args.putBoolean(NEED_REFRESH, needRefresh);
        ContentListFragment fragment = new ContentListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void iniView(View rootView, @Nullable Bundle savedInstanceState) {

        Bundle bundle = getArguments();

        needRefresh = bundle.getBoolean(NEED_REFRESH, true);

        if (needRefresh) {
            sf_layout.setColorSchemeResources(R.color.second_degree_title_text_blue,R.color.colorPrimary_blue,R.color.colorPrimaryDark_blue);

            sf_layout.setSize(SwipeRefreshLayout.LARGE);

            sf_layout.setOnRefreshListener(this::onRefresh);

            sf_layout.setProgressViewOffset(false, (int) (-20 * ScreenUtil.getDensity(mContext)), (int) (70 * ScreenUtil.getDensity(mContext)));

        }else{
            sf_layout.setEnabled(false);
        }

        ListContentAdapter adapter = new ListContentAdapter(mContext);
        adapter.addDatas(models);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(this::onItemClick);
    }

    private void onRefresh() {
        new Handler().postDelayed(() -> sf_layout.setRefreshing(false), 3000);
    }

    public void onItemClick(View view, int position){
        if (callBack != null) {
            callBack.addContentDetailFragment();
        }
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        for (int i=0;i<30;i++) {
            models.add(new ListContentModel(i, "sss", i % 3 + 1));
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean(SHWO_TOOL_BAR, showToolbar);
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.layout_of_fragment_content_list;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof ContentListFragmentCallBack) {
            callBack = (ContentListFragmentCallBack) context;
        }
    }

    @Override
    protected RxBasePresenter createPresenter() {
        return null;
    }

    @Override
    protected boolean userPresenter() {
        return false;
    }

    public interface ContentListFragmentCallBack{
        void addContentDetailFragment();
    }
}
